import { defineStore } from "pinia";
import { useBrowserStore } from "./BrowserStore";
import MessageUtil from "@/utils/MessageUtil";
import { useLoadingStore } from "@/store/LoadingStore";
import MessageBoxUtil from "@/utils/MessageBoxUtil";
import MessageEventEnum from "@/enumeration/MessageEventEnum";
import emitter from "@/plugins/mitt";

/**
 * 渲染目标路径
 * @param source 原始路径
 * @param path 目标目录
 */
function renderDestination(source: string, path: string): string {
    let nameIndex = source.lastIndexOf('/');
    if (nameIndex > -1) {
        return path + source.substring(nameIndex);
    }
    return path + '/' + source;
}

export const useOptionStore = defineStore('browser-option', {
    state: () => ({
        // 临时区
        temp: new Array<string>(),
        // 操作区
        option: new Array<string>(),
        isMove: true,
        // 执行操作的位置
        path: ''
    }),
    getters: {
        copy: (state): boolean => state.temp.length > 0,
        move: (state): boolean => state.temp.length > 0,
        remove: (state): boolean => state.temp.length > 0,
        paste: (state): boolean => state.option.length > 0 && state.path !== useBrowserStore().path
    },
    actions: {
        setTemp(temp: Array<string>) {
            this.temp = temp;
        },
        execCopy() {
            this.option = this.temp;
            this.temp = new Array<string>();
            this.isMove = false;
            this.path = useBrowserStore().path;
            emitter.emit(MessageEventEnum.CLEAR_OPTION);
        },
        execMove() {
            this.option = this.temp;
            this.temp = new Array<string>();
            this.isMove = true;
            this.path = useBrowserStore().path;
            emitter.emit(MessageEventEnum.CLEAR_OPTION);
        },
        async execPaste() {
            let path = useBrowserStore().path;
            useLoadingStore().start("开始黏贴")
            try {
                if (this.isMove) {
                    for (let item of this.option) {
                        let destination = renderDestination(item, path);
                        try {
                            useLoadingStore().setText(`【${item}】=>【${destination}】`);
                            await useBrowserStore().driver.moveFile(item, destination);
                        } catch (e) {
                            MessageUtil.error(`移动【${item}】到【${destination}】错误`, e);
                        }
                    }
                } else {
                    for (let item of this.option) {
                        try {
                            useLoadingStore().setText(`【${item}】=>【${path}】`);
                            await useBrowserStore().driver.copyFile(item,
                                renderDestination(item, path));
                        } catch (e) {
                            MessageUtil.error(`复制【${item}】到【${path}】错误`, e);
                        }
                    }
                }
                MessageUtil.success("黏贴成功");
                // 清空操作区
                this.option = new Array<string>();
                this.path = '';
            } catch (e) {
                MessageUtil.error(`黏贴时发生错误`, e);
            } finally {
                useLoadingStore().close();
                useBrowserStore().render();
            }
        },
        async execRemove() {
            await MessageBoxUtil.confirm(
                `此操作将彻底删除这【${this.temp.length}】个文件，是否继续？`,
                "删除警告", {
                confirmButtonText: "删除",
                cancelButtonText: "取消"
            });
            useLoadingStore().start("开始删除")
            try {
                for (let item of this.temp) {
                    useLoadingStore().setText(`正在删除【${item}】`);
                    await useBrowserStore().driver.rm(item);
                }
                MessageUtil.success("删除成功");
                // 清空临时区
                this.temp = new Array<string>();
                this.path = '';
                emitter.emit(MessageEventEnum.CLEAR_OPTION);
            } finally {
                useLoadingStore().close();
                useBrowserStore().render();
            }
        }
    }
})